home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1833 / 1833.xpi / chrome / yoono.jar / content / yoono / dialogs / restoreBkm.js < prev    next >
Text File  |  2009-12-16  |  4KB  |  166 lines

  1. /*
  2.  * restoreBkm.js
  3.  *
  4.  * @author    alexandre poirot <poirot.alex@gmail.com>
  5.  * @copyright 2005-2008 Yoono
  6.  */
  7.  
  8. const CI = Components.interfaces;
  9. const CL = Components.classes;
  10.  
  11. const PREFS  = CL['@mozilla.org/preferences-service;1'].getService(CI.nsIPrefBranch);
  12. const PROMPT = CL["@mozilla.org/embedcomp/prompt-service;1"].getService(CI.nsIPromptService);
  13.  
  14. Components.utils.import("resource://yoono/yoonoBkmSync.js");
  15. var gBkmService = YOONO_BKM;
  16.  
  17. var gBundle = null;
  18.  
  19.  
  20. function onSelectBackup(elt) {
  21. try {
  22.     var btn=document.getElementById('acceptbtn');
  23.     if (elt) {
  24.         var iframe=document.getElementById('backup-preview');
  25.         iframe.setAttribute("src","file://"+elt.backup.file.path);
  26.         btn.removeAttribute("disabled");
  27.     } else {
  28.         btn.setAttribute("disabled","true");
  29.     }
  30. } catch(e) {
  31.     alert(e);
  32. }
  33. }
  34.  
  35. function onChangeMaxHistory(value) {
  36.     
  37.     gBkmService.setMaxBackups(value);
  38.     reloadBackups();
  39.     
  40. }
  41.  
  42. function onAcceptDialog()
  43. {
  44.     var list=document.getElementById("backups-list");
  45.     var selected=list.selectedItem;
  46.     // Ask confirmation for replacing BookMarkFile
  47.     if(selected && confirm(gBundle.getString("replacebookmarks.confirm"))) 
  48.     {    
  49.         gBkmService.restore(selected.backup.name);
  50.     }
  51. }
  52.  
  53. function onCancelDialog()
  54. {
  55.   // Nothing to do
  56. }
  57.  
  58.  
  59. // Load backup list
  60. function reloadBackups() {
  61.     
  62.     var list=document.getElementById("backups-list"); 
  63.     while(list.firstChild)
  64.         list.removeChild(list.firstChild);
  65.     
  66.     var backups = gBkmService.getBackupList();
  67.     
  68.     for(var i=0;i<backups.length;i++) {
  69.         var b=backups[i];
  70.         var item=document.createElement("listitem");
  71.         item.setAttribute("label", b.date.toLocaleString());
  72.         item.backup=b;
  73.         list.appendChild(item);
  74.     }
  75.     
  76.     var iframe=document.getElementById('backup-preview');
  77.     iframe.setAttribute("src","about:blank");
  78.     
  79. }
  80.  
  81. function onInitDialog()
  82. {
  83. try {
  84.     
  85.     gBundle = document.getElementById("bundle");
  86.     
  87.     reloadBackups();
  88.     
  89.     // Set current history file number
  90.     var historyList=document.getElementById("maxhistorymenu"); 
  91.     historyList.selectedIndex = (gBkmService.getMaxBackups()/5)-1;
  92.     
  93.     
  94.     // Init iframe and watch for document loading
  95.     var iframe=document.getElementById('backup-preview');
  96.     iframe.addEventListener("DOMContentLoaded",function (event) {
  97.             try {
  98.                 var doc = event.originalTarget;
  99.                 
  100.                 // Add a css stylesheet and a javascript to make a dynamic tree
  101.                 var css=doc.createElement("link");
  102.                 css.setAttribute("rel","stylesheet");
  103.                 css.setAttribute("href","chrome://yoono/skin/mktree.css");
  104.                 css.setAttribute("type","text/css");
  105.                 doc.documentElement.appendChild(css);
  106.                 
  107.                 var js=doc.createElement("script");
  108.                 js.setAttribute("src","chrome://yoono/content/dialogs/mktree.js");
  109.                 js.setAttribute("type","text/javascript");
  110.                 doc.documentElement.appendChild(js);
  111.                 
  112.                 // Transform the original html which is very dirty to a clean <ul><li> list
  113.                 function transformHTML(DL) {
  114.                     try {
  115.                         
  116.                         var list=doc.createElement("UL");
  117.                         for(var i=0 ; i<DL.childNodes.length ; i++) {
  118.                             var child=DL.childNodes[i];
  119.                             
  120.                             if (child.tagName=="H3") { // we hit a folder
  121.                                 
  122.                                 var item=doc.createElement("LI");
  123.                                 item.textContent=child.textContent;
  124.                                 var sub=DL.childNodes[i+2];
  125.                                 if (sub.tagName=="DD")
  126.                                     item.appendChild(transformHTML(sub.childNodes[1]));
  127.                                 else if (sub.tagName=="DL")
  128.                                     item.appendChild(transformHTML(sub));
  129.                                 else
  130.                                     alert("bad sub : "+sub.tagName);
  131.                                 list.appendChild(item);
  132.                                 i+=1; // ignore folder's childs ...
  133.                             } else if (child.tagName=="DT" && child.firstChild && child.firstChild.tagName=="A") { // we hit a bookmark
  134.                                 var item=doc.createElement("LI");
  135.                                 item.textContent=child.firstChild.textContent;
  136.                                 list.appendChild(item);
  137.                             }
  138.                         }
  139.                         return list;
  140.                     } catch(e) {
  141.                         alert("transform : "+e);
  142.                     }
  143.                 }
  144.                 
  145.                 var DL=doc.body.childNodes[2];
  146.                 if (DL) {
  147.                     var list=transformHTML(DL);
  148.                     list.setAttribute("class","mktree");
  149.                     
  150.                     while(doc.body.firstChild)
  151.                         doc.body.removeChild(doc.body.firstChild);
  152.                     
  153.                     doc.body.appendChild(list, DL);
  154.                 }
  155.                 
  156.             } catch(e) {
  157.                 alert("load : "+e);
  158.             }
  159.         }, true);
  160.         
  161. } catch(e) {
  162. alert(e);
  163. }
  164.   
  165. }
  166.